import 'dart:convert';

import 'package:flutter/material.dart';

// import 'package:flutter_html/flutter_html.dart';
import 'package:get/get.dart';
import 'package:nft_shop/my/conteroller/web_conteroller.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/status_view.dart';
import 'package:webview_flutter/webview_flutter.dart';

class DetailsWebPage extends StatefulWidget {
  String name;

  DetailsWebPage({required this.name});

  @override
  _DetailsWebPageState createState() => _DetailsWebPageState();
}

WebViewController? _webViewController;

class _DetailsWebPageState extends State<DetailsWebPage> {
  String content = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // 用户协议：member_deal，
    // 购买协议：purchase_agreement ，
    // 售卖协议：sale_agreement ，
    // 平台介绍：platform_introduction ，
    // 联系我们：connect_us
    // trilateral_platform_fee：三方平台结算服务费，
    // platform_fe：平台服务费，
    // trilateral_platform_transfer_fee：三方平台转账手续费
    if (widget.name == "用户协议") {
      WebConteroller.to.getConfigNameDetail("member_deal");
    } else if (widget.name == "购买协议") {
      WebConteroller.to.getConfigNameDetail("purchase_agreement");
    } else if (widget.name == "售卖协议") {
      WebConteroller.to.getConfigNameDetail("sale_agreement");
    } else if (widget.name == "平台介绍") {
      WebConteroller.to.getConfigNameDetail("platform_introduction");
    } else if (widget.name == "联系我们") {
      WebConteroller.to.getConfigNameDetail("connect_us");
    } else if (widget.name == "三方平台结算服务费") {
      WebConteroller.to.getConfigNameDetail("trilateral_platform_fee");
    } else if (widget.name == "平台服务费") {
      WebConteroller.to.getConfigNameDetail("platform_fe");
    } else if (widget.name == "三方平台转账手续费") {
      WebConteroller.to.getConfigNameDetail("trilateral_platform_transfer_fee");
    }else if (widget.name == "数字资产交易服务条款") {
      WebConteroller.to.getConfigNameDetail("number_service_agreement");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: widget.name,
      ),
      body: GetBuilder<WebConteroller>(
        builder: (logic) {
          if (logic.status != LoadStatusType.success) {
            return StatusViews(logic.status);
          }

          // return MyScrollView(
          //     padding: EdgeInsets.all(10),
          //     children:[
          //       Html(
          //         data: logic.article!.value,
          //       // ),
          //     ]
          // );

          return WebView(
            initialUrl: '',
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              _webViewController = webViewController;
              _loadHtmlFromAssets(logic.article!.value);
            },
          );
        },
      ),
    );
  }

  _loadHtmlFromAssets(String? value) async {
    _webViewController!.loadUrl(Uri.dataFromString(value ?? "",
            mimeType: 'text/html', encoding: Encoding.getByName('utf-8'))
        .toString());
  }
}
